// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // 主界面parent
        mainPanel:cc.Node,
        // 子界面parent
        subPanel:cc.Node,

        panelList:{
            type:cc.Node,
            default:[],
        },

        rankList:{
            type:cc.SpriteFrame,
            default:[],
        },
        cakeList:{
            type:cc.SpriteAtlas,
            default:null,
        },

        ggkGiftList:{
            type:cc.SpriteAtlas,
            default:null,
        },

        userHeadList:{
            type:cc.SpriteFrame,
            default:[],
        }
    },

    

     onLoad(){
      

      this.initNode(this.mainPanel,true);
      this.initNode(this.subPanel,false);
      cc.game.on(cc.game.EVENT_HIDE,this.onGameHide,this);
      cc.game.on(cc.game.EVENT_SHOW,this.onGameShow,this);
      cc.tg.eventCenter.addEvent("showPanel",(data)=>{
          if(data == null)
            return ;

          this.showPanel(data);
      });
      cc.tg.eventCenter.addEvent("onUpdateInfo",()=>{
          
      })
      cc.log("init done !!!");
    //   //测试代码
    //   setInterval(()=>{
    //       let data = {
    //           info:{
    //             pos:0,
    //           }
    //       }

    //       cc.tg.eventCenter.dispatchEvent("onGainCoin",data)
    //   },3000)

    //   setTimeout(()=>{
    //       cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true})
    //   },1000)

      //测试代码
    // setTimeout(()=>{
    //     cc.tg.eventCenter.dispatchEvent("onHBRain");
    // },5000 )


    //   setTimeout(()=>{
      
    //     cc.tg.eventCenter.dispatchEvent("showCoin");
        
    //   },500)
      cc.tg.audioMgr.play("leshphon.mp3",true);


      cc.tg.gameLogic.toGetUserInfo();
      cc.tg.gameLogic.toGetAccInfo();
      cc.tg.gameLogic.toGetGGKList();
      cc.tg.gameLogic.toGetRandomCoin();
      cc.tg.gameLogic.toGetluckyWheel();
    },  


    initNode(node,isActive = false){
        for(let i = 0 ; i < node.childrenCount;++i)
        {
            let child = node.children[i];
            let panel = child.getComponent("panelBase");
            if(panel)
            {
                
                panel.init();
                if(isActive)
                {
                    panel.onShow();

                }
                else
                {
                    panel.onHide();

                }
                this.panelList.push(child);
            }
        }
    },

    //控制界面的显示和隐藏
    showPanel(data){
        let pName = data.panelName;
        let active = data.active;
        for(let key in this.panelList)
        {
            let panel = this.panelList[key]
            if(pName == panel.name)
            {
                if(active)
                {
                    panel.getComponent("panelBase").onShow();
                }
                else
                {
                    panel.getComponent("panelBase").onHide();
                }
            }
        }

    },

    onGameHide(){
        
    },

    onGameShow(){
       // cc.tg.adDataCenter.toShowAd("lx");
    },
    
    onDestroy(){
        cc.game.off(cc.game.EVENT_HIDE,this.onGameHide,this);
        cc.game.off(cc.game.EVENT_SHOW,this.onGameShow,this);
    }
});
